export


PLATFORM:=$(shell uname)
FILE_LOCATION:=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
ifneq ($(PLATFORM),Linux)
FILE_LOCATION_ASSIST:=$(subst /, ,$(FILE_LOCATION))
ifeq ($(word 1,$(FILE_LOCATION_ASSIST)),cygdrive)
FILE_LOCATION:=$(patsubst /cygdrive/$(word 2,$(FILE_LOCATION_ASSIST))%,$(word 2,$(FILE_LOCATION_ASSIST)):%,$(FILE_LOCATION))
endif
endif
THEBUILDER_REPO_LOCATION:=$(FILE_LOCATION)
PATH_INCLUDE:=$(addprefix $(FILE_LOCATION)../,$(shell if [ -e $(FILE_LOCATION)../path_include.txt ];then cat $(FILE_LOCATION)../path_include.txt;fi))
PATH_LIB:=$(addprefix $(FILE_LOCATION)../,$(shell if [ -e $(FILE_LOCATION)../path_lib.txt ];then cat $(FILE_LOCATION)../path_lib.txt;fi))


define getTheMM
$(shell if [ $2 ];then echo $(filter %.$1,$(shell g++ -MM $(addprefix -I,$3) $2 $(FILE_LOCATION)makefiles/hold.cpp));fi)
endef

define get_fullpath
$(shell
	result=;
	for lib in $1;do
		for libpath in $2;do
			cd $$libpath;
			got=;
			if [ ! $$got ] && [ -e $$lib ];then
				result="$$result $(addprefix $$libpath/,$(subst $$libpath/,,$$lib))";
				got="got";
			fi;
			if [ ! $$got ] && [ $$libpath == $(lastword $2) ];then
				result="$$result $$lib";
			fi;
		done;
	done;
	echo $$result;
)
endef

define getType
$(shell
	suffix=$(word 2,$(subst ., ,$(notdir $1)));
	if [ ! $$suffix ] || [ $$suffix = exe ]; then
		echo executebin;
	elif [ $$suffix = a ] || [ $$suffix = lib ]; then
		echo staticlib;
	elif [ $$suffix = so ] || [ $$suffix = dll ]; then
		echo sharedlib;
	else
		echo unknow;
	fi;
)
endef

ifeq ($(PLATFORM),Linux)
obj_suffix:=.o
lib_prefix:=lib
lib_suffix:=.a
dll_prefix:=lib
dll_suffix:=.so
else
obj_suffix:=.obj
lib_suffix:=.lib
dll_suffix:=.dll
exe_suffix:=.exe
endif

formObj=$(1)$(obj_suffix)
formLib=$(dir $(1))$(lib_prefix)$(notdir $(1))$(lib_suffix)
formDll=$(dir $(1))$(dll_prefix)$(notdir $(1))$(dll_suffix)
formExe=$(1)$(exe_suffix)

#---------| template |---------

# TARGETi=xxx# lib or bin

# TARGETi_FLAKEj=xxx# object file
# # ~~
# TARGETi_FLAKEj_D=MACRO_1=value1\
#  MACRO_2=value2\
#  MACRO_3=value3\
#  ...
# TARGETi_FLAKEj_source=xxx# corresponding source code
# TARGETi_FLAKEj_header=xxx ...# every related header
# TARGETi_FLAKEj_inline=xxx ...# every related inline file
# TARGETi_FLAKEj_library=xxx xxx:xxx ...# every related lib
# # OR
# TARGETi_FLAKEj_EQUAL=TARGETm_FLAKEn# flake call is diff but other all are the same
# # ~~

# ...
# #
# # statistic for make the last part be easy. This part should use '=' rather than ":="
# TARGETi_FLAKEs=TARGETi_FLAKEj ...
# TARGETi_FLAKEs_VARs=$(TARGETi_FLAKEj) ...
# TARGETi_FLAKEs_LIBs=$(call get_fullpath,$(TARGETi_FLAKEj_library) ...,$(PATH_LIB))
# TARGETi_TYPE=$(call getType,$(TARGETi))
# #
#---------| template |---------


#---------|   demo   |---------

TARGET0:=$(call formExe,$(FILE_LOCATION)demo/output/demo)

TARGET0_FLAKE1:=$(call formObj,$(FILE_LOCATION)demo/output/demo)
TARGET0_FLAKE1_D:=CONTENT=\"Hello_world!\"
TARGET0_FLAKE1_source:=$(FILE_LOCATION)demo/demo.cpp
TARGET0_FLAKE1_header:=$(call getTheMM,hpp,$(TARGET0_FLAKE1_source))
#
TARGET0_FLAKEs:=TARGET0_FLAKE1
TARGET0_FLAKEs_VARs:=$(TARGET0_FLAKE1)
TARGET0_TYPE:=$(call getType,$(TARGET0))
#

DIRs:=$(FILE_LOCATION)demo/output
#---------|   demo   |---------


ifeq ($(strip $(MAKECMDGOALS)),)
target_index:=0
endif

# > > > > > > > > > > > > > > >
